package com.xand.jsonrpc.server;

import java.lang.reflect.Method;
import java.util.Map;

import com.xand.jsonrpc.JSONGenericRequest;
import com.xand.jsonrpc.JSONGenericResponse;

public class JSONServiceDefinition {
	private String id;
	private Method method;
	
	private String executorClass;
	private Object instance;
	
	private String in;
	
	private String out;

	public void startExecutor() throws Exception {
		if(instance == null) {
			Class clazz = Class.forName(executorClass);
			instance = clazz.newInstance();
		}
		
		Method[] ms = instance.getClass().getMethods();
		for(Method m : ms) {
			if(m.getName().equals(id)) {
				method = m;
			}
		}
	}
	
	public JSONGenericResponse invoke(JSONGenericRequest req, Map context) throws Exception {
		// aqui es donde voy a hacer el casting
		Class clazz = Class.forName(in);
		
		return (JSONGenericResponse)method.invoke(instance, new Object[]{req.getRequest(clazz), context});
	}
	
	public Object getInstance() {
		return instance;
	}
	
	public void setInstance(Object instance) {
		this.instance = instance;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getExecutorClass() {
		return executorClass;
	}

	public void setExecutorClass(String executorClass) {
		this.executorClass = executorClass;
	}

	public String getIn() {
		return in;
	}

	public void setIn(String in) {
		this.in = in;
	}

	public String getOut() {
		return out;
	}

	public void setOut(String out) {
		this.out = out;
	}
}
